package org.com.action.user;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;
import org.com.common.LogInfo;
import org.com.dao.RegistDao;
import org.com.domain.User;
import org.com.exception.LoginErrorException;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;


@SuppressWarnings("serial")
public class UserLoginAction extends ActionSupport implements Preparable, SessionAware, ModelDriven<User>{

	User user;
	List<User> userList;
	RegistDao registDao;
	Map<String, Object> session;
	
	public User getUser() {
		
		return user;
	}

	@Override
	public String execute() throws Exception {
		this.userList = registDao.getUserList();
		for (User person : userList) {
			if(person.getName().equals(user.getName())&&person.getPassword().equals(user.getPassword())){
				session.put("my_info", person);
				LogInfo.getLogSessionInfo(session.get("my_info"));
				
				return SUCCESS;
			}			
		}
		throw new LoginErrorException("Please, Confirm Your Name and Password");
		
	}
	
	public String logOut() throws Exception {
		session.remove("my_info");
		session.clear();
		return SUCCESS;
	}


	public void prepare() throws Exception {
		user = new User();
		registDao = new RegistDao();
		userList = new ArrayList<User>();
		
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
		
	}

	public User getModel() {
		// TODO Auto-generated method stub
		return user;
	}


	
	

}
